<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
  <head>
    <meta charset="UTF-8">
    <title>授权页面-Long-admin</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link  th:href="@{/X-admin/lib/zTree/css/bootstrapStyle/bootstrapStyle.css}" rel="stylesheet" type="text/css">
    <link  th:href="@{/X-admin/css/font.css}" rel="stylesheet" type="text/css">
    <link  th:href="@{/X-admin/css/xadmin.css}" rel="stylesheet" type="text/css">

  </head>
  <body>
    <div class="x-nav">
      <input id="getValue" type="hidden"/>
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">UPMS</a>
        <a>
          <cite>授权</cite></a>
      </span>

      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">&#x1002;</i></a>
    </div>
    <div class="x-body">
      <ul id="treeDemo" class="ztree">

      </ul>
      <button  id="btn" class="layui-btn" data-type="edit">确定</button>
    </div>

    <script type="text/javascript"  th:attr="src=@{/X-admin/lib/jquery-3.2.1.min.js}"></script>
    <script type="text/javascript"  th:attr="src=@{/X-admin/lib/layui/layui.js}"></script>
    <script type="text/javascript"  th:attr="src=@{/X-admin/js/xadmin.js}"></script>

    <script type="text/javascript" th:attr="src=@{/X-admin/lib/zTree/js/jquery.ztree.core.js}"></script>
    <script type="text/javascript" th:attr="src=@{/X-admin/lib/zTree/js/jquery.ztree.excheck.js}"></script>
    <script type="text/javascript" th:attr="src=@{/X-admin/lib/zTree/js/jquery.ztree.exedit.js}"></script>

    <script type="text/javascript"  th:attr="src=@{/js/hylUtil.js}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

  </body>
</html>
<script>
    var setting = {
        view: {
            selectedMulti: false
        },
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pid",
                rootPId:null
            }
        },
        edit: {
            enable: true
        }
    };

    var zNodes =[];

    $(function () {
        var obj=parent.$("#getValue").data("obj")[0]
        $.ajax({
            url:'/roleController/allTree?userId='+obj.id,
            type:'post',
            data:{},
            success:function (result) {
                if (result!=null){
                    zNodes=result;
                    $(document).ready(function(){
                        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                    });
                }
            },
            dataType:'json'
        });


    });
</script>

<script type="text/javascript">
    //将选择的值 L_pid
    $(function () {
        $("#btn").click(function () {
            var ids="",roleIds="";
//              获取当前被勾选的节点集合
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var nodes = treeObj.getCheckedNodes(true);
//            父页面
            var obj=parent.$("#getValue").data("obj");
            if (obj.length>0){
              for (var j=0;j<obj.length;j++){
                  ids+=obj[j].id+",";
              }
            }
            if (nodes.length>0){
                for(var i=0;i<nodes.length;i++){
                    roleIds+=nodes[i].id + ",";
                }
                $.ajax({
                    url:'/userController/grant',
                    data:{ids:ids,roleIds:roleIds},
                    type:'post',
                    dataType:'json'
                });


                // 获得frame索引
                var index = parent.layer.getFrameIndex(window.name);
                //关闭当前frame
                parent.layer.close(index);
            }else{
                layer.msg("请选择权限");
            }
//              layer.msg(JSON.stringify(nodes));
        });
    });
</script>